package com.tjetc.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageInfo;
import com.tjetc.common.Result;
import com.tjetc.entity.ActivityType;
import com.tjetc.entity.TribeMessage;
import com.tjetc.entity.User;
import com.tjetc.entity.UserTribe;
import com.tjetc.service.ITribeMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


@RestController
@RequestMapping("/tribeMessage")
public class TribeMessageController {
    @Autowired
    private ITribeMessageService tribeMessageService;

    @RequestMapping("/joinTribe")
    public Result<?> joinTribe(Integer uid, Integer tid, String reason) {
        System.out.println("uid = " + uid + ", tid = " + tid + ", reason = " + reason);
        QueryWrapper<TribeMessage> qw = new QueryWrapper<>();
        qw.eq("user_id", uid);
        qw.eq("tribe_id", tid);
        TribeMessage tribeMessage1 = tribeMessageService.getOne(qw);
        if (tribeMessage1 != null) {
            if (tribeMessage1.getState() == 0) {
                return Result.fail("请勿重复申请");
            } else  {
                return Result.fail("您已加入此部落");
            }


        } else {


            TribeMessage tribeMessage = new TribeMessage();
            tribeMessage.setTribeId(tid);
            tribeMessage.setUserId(uid);
            tribeMessage.setContent(reason);
            tribeMessage.setState(0);
            boolean save = tribeMessageService.saves(tribeMessage);

            if (save) {
                return Result.success();
            } else {
                return Result.fail();
            }


        }
    }

    @RequestMapping("/exits")
    public Result<?> exits(Integer uid, Integer tid) {
        System.out.println("uid = " + uid + ", tid = " + tid );
        QueryWrapper<TribeMessage> qw = new QueryWrapper<>();
        qw.eq("user_id", uid);
        qw.eq("tribe_id", tid);
        TribeMessage tribeMessage1 = tribeMessageService.getOne(qw);
        boolean flag=tribeMessageService.exits(tribeMessage1);
        if (flag){
            return Result.success();
        }else{
            return Result.fail();
        }

    }

    @RequestMapping("/list")
    public PageInfo<TribeMessage> list(@RequestParam(defaultValue = "1") Integer curPage,
                                       @RequestParam(defaultValue = "10") Integer pageSize,
                                       @RequestParam(defaultValue = "10" ) String applyId){
        return  tribeMessageService.lists(curPage,pageSize,applyId);

    }

    @RequestMapping("/check")
    public Result<?> check(Integer id) {
        boolean i = tribeMessageService.check(id);

        if (i) {
            return Result.success("成功");
        } else {
            return Result.fail("失败");
        }

    }


}





